package com.atguigu.ucenterservice.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.servicebase.exceptionHandler.GuliException;
import com.atguigu.ucenterservice.entity.RegisterVo;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.mapper.UcenterMemberMapper;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-25
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(UcenterMember ucenterMember) {
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        //手机号和密码为空
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password))
            throw new GuliException(20001,"登陆失败");

        //检查是否有该用户
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(wrapper);
        if(member == null)
            throw new GuliException(20001,"登陆失败");

        //检查密码是否正确
        if(!MD5.encrypt(password).equals(member.getPassword()))
            throw new GuliException(20001,"登陆失败");

        //检查该用户是否被禁用
        if(member.getIsDisabled())
            throw new GuliException(20001,"登陆失败");

        //登录成功
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }

    //注册
    @Override
    public boolean register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //检查手机号和密码是否为空
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(password ) || StringUtils.isEmpty(code))
            throw new GuliException(20001,"注册失败");

        //检查验证码是否正确
        String registerCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(registerCode))
            throw new GuliException(20001,"注册失败");

        //检查该用户是否已注册
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0)
            throw new GuliException(20001,"注册失败");

        //注册
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setAvatar("https://guli-edu-20201.oss-cn-beijing.aliyuncs.com/2020/10/08/3a6bf3d4a85f415693e062db5fb17df8file.png");
        ucenterMember.setIsDisabled(false);
        int i = baseMapper.insert(ucenterMember);
        if(i == 0)
            return false;
        return true;

    }
}
